import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository, Not, IsNull } from 'typeorm'
import { Admin } from '../admin/entities/admin.entity'
import { Staff } from '../staff/entities/staff.entity'
import { WorkState } from '../work-state/entities/work-state.entity'
import { CreateApplyDto } from './dto/create-apply.dto'
import { UpdateApplyDto } from './dto/update-apply.dto'
import { Apply } from './entities/apply.entity'
import { formatDate, DateUtil } from 'src/utils'
import { ApplyStateEnum } from 'src/types/apply'
@Injectable()
export class ApplyService {
  constructor(
    @InjectRepository(Apply) private readonly apply: Repository<Apply>,
    @InjectRepository(Admin) private readonly admin: Repository<Admin>,
    @InjectRepository(Staff) private readonly staff: Repository<Staff>,
    @InjectRepository(WorkState)
    private readonly workState: Repository<WorkState>
  ) {}
  async create(createApplyDto: CreateApplyDto) {
    const apply = new Apply()
    const staff = await this.staff.findOne({
      where: { id: createApplyDto.staffId }
    })
    const workState = await this.workState.findOne({
      where: { id: createApplyDto.workStateId }
    })
    apply.staff = staff
    apply.workState = workState
    apply.applyDate = new DateUtil(new Date(), 'yyyy-MM-dd hh:mm:ss').format()
    apply.reason = createApplyDto.reason
    apply.detail = createApplyDto.detail
    apply.content = createApplyDto.content
    apply.state = '' + ApplyStateEnum['审核中']
    return this.apply.save(apply)
  }

  async findAll(
    params: {
      page: number
      pageSize: number
      state?: string
      isDelete?: boolean
    },
    staffId?: number
  ) {
    const where = {}
    params.isDelete && (where['deleteDate'] = Not(IsNull()))
    params.state && (where['state'] = params.state)
    staffId && (where['staff'] = { id: staffId })
    const data = await this.apply.find({
      where: { ...where },
      relations: ['staff', 'workState', 'admin'],
      withDeleted: params.isDelete,
      skip: (params.page - 1) * params.pageSize,
      take: params.pageSize,
      order: { id: 'DESC' }
    })
    const total = await this.apply.count({
      where: { ...where },
      relations: ['staff'],
      withDeleted: params.isDelete
    })
    const newData = data.map(item => {
      return {
        ...item,
        staff: { id: item.staff.id, name: item.staff.name },
        workState: { id: item.workState.id, name: item.workState.name },
        admin: item.admin ? { id: item.admin.id, name: item.admin.name } : null
      }
    })
    return { data: newData, total }
  }

  async findOne(id: number) {
    return await this.apply.findOne({ where: { id } })
  }

  async update(id: number, updateApplyDto: UpdateApplyDto) {
    const { adminId, workStateId, ...rest } = updateApplyDto
    const admin = await this.admin.findOne({ where: { id: adminId } })
    const workState = await this.workState.findOne({
      where: { id: workStateId }
    })
    const verifyDate = new DateUtil(new Date(), 'yyyy-MM-dd hh:mm:ss').format()
    const apply = {}
    admin && (apply['admin'] = admin)
    workState && (apply['workState'] = workState)
    return this.apply.update(id, { ...rest, ...apply, verifyDate })
  }

  remove(ids: number[]) {
    return this.apply.softDelete(ids)
  }

  thoroughRemove(ids: number[]) {
    return this.apply.delete(ids)
  }

  restore(ids: number[]) {
    return this.apply.restore(ids)
  }
}
